/** @format */

const { Store, Docs } = require("../model/index");
/**
 * @format
 * @param {Request} req
 * @param {Response} res
 */
module.exports.editDocs = async (req, res, next) => {
  // console.log(req.body);
  try {
    const dbb = await Docs.findOneAndUpdate({ _id: req.body.id }, req.body, {
      new: true,
    });
    if (dbb) {
      res.status(201);
      res.data = dbb;
      res.msg = "更新成功";
    } else {
      res.status(204);
    }
  } catch (error) {
    res.status(500);
    res.data = error;
    res.msg = "更新失败";
  }
  next();
};
module.exports.delDocs = async (req, res, next) => {
  try {
    const dbb = await Docs.findOneAndDelete({ _id: req.query.id });
    if (dbb) {
      res.status(200);
      res.data = dbb;
      res.msg = "删除成功";
    } else {
      res.status(204);
    }
  } catch (error) {
    res.status(500);
    res.data = error;
    res.msg = "删除失败";
  }
  next();
};
module.exports.addDocs = async (req, res, next) => {
  const docs = new Docs(req.body);
  const dbb = await docs.save();
  res.data = dbb;
  res.msg = "添加成功";
  res.status(201);
  next();
};

module.exports.getDocs = async (req, res, next) => {
  const dbb = await Docs.find(req.query);
  res.status(200);
  res.msg = "获取成功";
  res.data = dbb;
  next();
};

module.exports.editStore = async (req, res, next) => {
  // console.log(req.body);
  try {
    const dbb = await Store.findOneAndUpdate({ _id: req.body.id }, req.body, {
      new: true,
    });
    if (dbb) {
      res.status(201);
      res.data = dbb;
      res.msg = "更新成功";
    } else {
      res.status(204);
    }
  } catch (error) {
    res.status(500);
    res.data = error;
    res.msg = "更新失败";
  }
  next();
};
module.exports.delStore = async (req, res, next) => {
  try {
    const dbb = await Store.findOneAndDelete({ _id: req.query.id });
    if (dbb) {
      res.status(200);
      res.data = dbb;
      res.msg = "删除成功";
    } else {
      res.status(204);
    }
  } catch (error) {
    res.status(500);
    res.data = error;
    res.msg = "删除失败";
  }
  next();
};

module.exports.getStore = async (req, res, next) => {
  const dbb = await Store.find();
  res.status(200);
  res.msg = "获取成功";
  res.data = dbb;
  next();
};
module.exports.addStore = async (req, res, next) => {
  const store = new Store(req.body);
  const dbb = await store.save();
  res.data = dbb;
  res.msg = "添加成功";
  res.status(201);
  next();
};
